package com.example.echoewish.ui.my_activity.user_info_activity;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.echoewish.R;
import com.example.echoewish.pojo.User;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.ui.EchoWishCompatActivity;

public class UserInfoEditActivity extends EchoWishCompatActivity {
    private static int UPDATE_SUCCESS=3;
    private static int UPDATE_FAIL=4;

    private TextView nameTextView;
    private Spinner sexSpinner;
    private Spinner campusSpinner;
    private Spinner gradeSpinner;
    private Button  infoEditButton;

    private UserInfoEditViewModel userInfoEditViewModel;

    private Handler userInfoEditHandler=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    if(msg.arg1==UPDATE_SUCCESS)
                    {
                        changeUserInfo();
                        Toast.makeText(UserInfoEditActivity.this,"资料更新成功",Toast.LENGTH_SHORT).show();
                    }
                    else if(msg.arg1==UPDATE_FAIL)
                        Toast.makeText(UserInfoEditActivity.this,"更新资料失败",Toast.LENGTH_SHORT).show();
                    break;
                case Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_user_info_edit);
    }

    public void initView()
    {
        //初始化控件
        nameTextView=findViewById(R.id.info_edit_name_edit_text);
        sexSpinner=findViewById(R.id.info_edit_sex_spinner);
        campusSpinner=findViewById(R.id.info_edit_campus_spinner);
        gradeSpinner=findViewById(R.id.info_edit_grade_spinner);
        infoEditButton=findViewById(R.id.info_edit_change_button);


        nameTextView.setText(UserInfo.getInstance().name);
        infoEditButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                User user=new User();
                user.userId=UserInfo.getInstance().UserId;
                user.name=nameTextView.getText().toString();
                if(user.name==null)
                {
                    Toast.makeText(UserInfoEditActivity.this,"名字不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                user.sex=sexSpinner.getSelectedItemPosition()-1;
                user.campus=campusSpinner.getSelectedItem().toString();
                user.grade=Integer.valueOf(gradeSpinner.getSelectedItem().toString());
                userInfoEditViewModel.userMutableLiveData.setValue(user);
            }
        });
    }

    @Override
    public void initViewModel() {
        userInfoEditViewModel=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(UserInfoEditViewModel.class);
    }

    @Override
    public void observeLiveData() {

        userInfoEditViewModel.userMutableLiveData.observe(this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                userInfoEditViewModel.updateUserInfo();
            }
        });

        userInfoEditViewModel.responseMutableData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (s.equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                {
                    Message msg=new Message();
                    msg.what=Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    msg.arg1=UPDATE_SUCCESS;
                    userInfoEditHandler.sendMessage(msg);
                }
                else {
                    Message msg=new Message();
                    msg.what=Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    msg.arg1=UPDATE_FAIL;
                    userInfoEditHandler.sendMessage(msg);
                }
            }
        });
    }

    //回调成功后更改 配置值
    private void changeUserInfo()
    {
        UserInfo.getInstance().name=nameTextView.getText().toString();
        UserInfo.getInstance().sex=sexSpinner.getSelectedItemPosition()-1;
        UserInfo.getInstance().campus=campusSpinner.getSelectedItem().toString();
        UserInfo.getInstance().grade = Integer.valueOf(gradeSpinner.getSelectedItem().toString());
    }

}